package com.example.k8s.hello.service.sys.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.k8s.hello.service.sys.dao.SysRoleDao;
import com.example.k8s.hello.service.sys.dao.SysUserDao;
import com.example.k8s.hello.service.sys.service.SysRoleMenuService;
import com.example.k8s.hello.service.sys.service.SysRoleService;
import com.example.k8s.hello.service.sys.service.SysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import com.example.k8s.hello.common.bean.Page;
import com.example.k8s.hello.bean.sys.entity.SysRoleEntity;
import com.example.k8s.hello.bean.sys.query.SysRoleQuery;
import com.example.k8s.hello.bean.sys.constant.SysConstant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 角色
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {
	private final SysRoleMenuService sysRoleMenuService;
	private final SysUserDao sysUserDao;
    private final SysUserRoleService sysUserRoleService;

	@Override
	public Page queryPage(SysRoleQuery query, Page page) {
		String roleName = query.getRoleName();

		return this.page(
				page,
				Wrappers.lambdaQuery(SysRoleEntity.class)
						.like(StringUtils.isNotBlank(roleName),SysRoleEntity::getRoleName, roleName)
//						.eq(!StpExtendUtil.ifAdmin(), SysRoleEntity::getCreateUserId, StpExtendUtil.getUserId())
		);
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(SysRoleEntity role) {
        this.save(role);

        //检查权限是否越权
        checkPerms(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRoleEntity role) {
        this.updateById(role);

        //检查权限是否越权
        checkPerms(role);

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        //删除角色
        this.removeByIds(Arrays.asList(roleIds));

        //删除角色与菜单关联
        sysRoleMenuService.deleteBatch(roleIds);

        //删除角色与用户关联
        sysUserRoleService.deleteBatch(roleIds);
    }


    @Override
	public List<Long> queryRoleIdList(Long createUserId) {
		return baseMapper.queryRoleIdList(createUserId);
	}

	/**
	 * 检查权限是否越权
	 */
	private void checkPerms(SysRoleEntity role){
		//如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
		if(role.getCreateUserId() == SysConstant.SUPER_ADMIN){
			return;
		}
		
		//查询用户所拥有的菜单列表
		List<Long> menuIdList = sysUserDao.queryAllMenuId(role.getCreateUserId());
		
		//判断是否越权
		if(!menuIdList.containsAll(role.getMenuIdList())){
			throw new RuntimeException("新增角色的权限，已超出你的权限范围");
		}
	}
}
